package com.google.code.sms.android.parser;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.code.sms.android.domain.Version;

/**
 * @author Sindre Mehus
 */
public class VersionParser {

    public Version parse(Reader reader) throws Exception {

        BufferedReader bufferedReader = new BufferedReader(reader);
        Pattern pattern = Pattern.compile("SMS_ANDROID_VERSION_BEGIN(.*)SMS_ANDROID_VERSION_END");
        String line = bufferedReader.readLine();
        while (line != null) {
            Matcher finalMatcher = pattern.matcher(line);
            if (finalMatcher.find()) {
                return new Version(finalMatcher.group(1));
            }
            line = bufferedReader.readLine();
        }
        return null;
    }
}